import projectConfig from '@/common/config.js';

const install = (Vue, vm) => {

	Vue.prototype.$tools = {
		// 转大写
		toUpperCase(arg) {
			return arg && arg.toUpperCase();
		},

		hideIdCardNumber(idCard) {
			if (!idCard) return ''
			if (idCard.length === 15) {
				return idCard.replace(/(\d{6})\d{6}(\d{3})/, '$1******$2')
			} else if (idCard.length === 18) {
				return idCard.replace(/(\d{6})\d{8}(\d{4})/, '$1********$2')
			}
			return idCard
		},

		hidePhoneNumber(phone) {
			if (!phone) {
				return ''
			}
			if (phone.length >= 7) {
				return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
			}
			return phone
		},

		// 转对象或数组对象
		parseJsonObject(value) {
			// return value && value !== null && Array.isArray(value)
			// return typeof value === 'object' && value !== null && !Array.isArray(value)
			if (!value || value === null) {
				return value
			} else if (Array.isArray(value)) {
				return value
			} else if (typeof value === 'object') {
				return value
			} else {
				return JSON.parse(value)
			}
		},

		// 对象属性排序
		sortJsonObject(obj) {
			// 排序顺序将是：数字、大写字母、下划线、小写字母
			const newObj = parseJsonObject(obj)
			const sortedObj = {}
			const sortedKeys = Object.keys(newObj).sort()
			sortedKeys.forEach((key) => {
				if (newObj[key] != undefined && newObj[key] != null) {
					if (typeof newObj[key] === 'object' || Array.isArray(newObj[key])) {
						sortedObj[key] = newObj[key]
					} else {
						sortedObj[key] = newObj[key].toString()
					}
				}
			})
			return sortedObj
		},

		// 输出完整URL
		buildUrl(url) {
			if (!url) {
				return ''
			}
			if (url.includes('http:') || url.includes('https:')) {
				return url
			}
			if (url.includes('blob:')) {
				return ''
			} else {
				return projectConfig.baseUrl + url
			}
		},

		baseUrl() {
			return projectConfig.baseUrl;
		},

		yyyymmddFormat(value) {
			if (value && value !== '') {
				return uni.$u.timeFormat(value, 'yyyy-mm-dd')
			}
			return value;
		}
	}
}

export default {
	install
}